<!--
 * @Author: ChunLai
 * @Date: 2022-03-29 17:14:07
 * @LastEditTime: 2025-08-22 09:38:22
 * @Description: 省市区地址选择
 * @FilePath: \02.bldinsure\src\components\Address.vue
-->
<template>
  <div class="addr-module bg-fff">
    <van-picker
      v-if="backups.length > 0"
      show-toolbar
      title="请选择地址"
      value-key="label"
      :columns="columns"
      @cancel="cancle"
      :default-index="defaultIndex"
      @confirm="confirm"
    />
    <template v-else>
      <div class="load-css" v-if="hasChildren">
        <van-loading type="spinner" color="#1989fa" size="24px"
          >数据加载中...</van-loading
        >
      </div>
    </template>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { findPostCode } from "@/utils/GetPostcode.js";

export default {
  name: "AddressModule",
  props: {
    filterCode: "",
  },
  data() {
    return {
      backups: [], //数据备份
      province: "请选择省份",
      city: "请选择城市",
      county: "请选择区县",
      tabIndex: 0,
      provinceIndex: "",
      cityIndex: "",
      countyIndex: "",
      provinceCode: "",
      cityCode: "",
      countyCode: "",
      addressDetail: "", //地址拼接详情
      addressCodeList: {
        addressDetail: "",
        provinceCode: "",
        cityCode: "",
        countyCode: "",
        provinceName: "",
        cityName: "",
        countyName: "",
        rgtAddress: "", //省市
        zipCode: "", //邮编
        townNo: "",
        townName: "",
      },
      cityList: "", //暂存的市
      hasChildren: true,
    };
  },
  computed: {
    ...mapGetters({
      provinceNo: "agent/saleProvinceNo",
    }),
    columns() {
      let data = JSON.parse(JSON.stringify(this.backups));
      if (this.filterCode) {
        data = data.filter((item) => item.value == this.filterCode);
      }
      return data;
    },
    defaultIndex() {
      let index = 0;
      if (this.provinceNo) {
        let sindex = this.columns.findIndex(
          (item) => item.value == this.provinceNo
        );
        if (sindex > 0) {
          index = sindex;
        }
      }
      return index;
    },
  },
  created() {
    this.$store.dispatch("json/getAreaJson").then((data) => {
      if (this.filterCode) {
        data = data.filter((item) => item.value == this.filterCode);
      }
      this.backups = data;
    });
  },
  methods: {
    resetData(list) {
      list.forEach((item) => {
        item.defaultIndex = 0;
        if (item.children) {
          if (item.children.length == 0) {
            delete item.children;
          } else {
            this.resetData(item.children);
          }
        }
      });
      return list;
    },
    // 确认,关闭弹窗
    confirm(value, data) {
      // console.log(value, data);
      const shengName = value[0],
        shiName = value[1],
        quName = value[2] || "",
        jieName = value[3] || "";
      this.addressCodeList.addressDetail = value.join("");
      this.addressCodeList.rgtAddress = shengName + shiName; //省市
      this.addressCodeList.provinceName = shengName;
      this.addressCodeList.cityName = shiName;
      this.addressCodeList.countyName = quName;
      this.addressCodeList.townName = jieName;
      // 编码
      const shengObj = this.columns[data[0]],
        shiObj = shengObj.children[data[1]];
      this.addressCodeList.provinceCode = shengObj.value;
      this.addressCodeList.cityCode = shiObj.value;
      if (data[2] !== undefined) {
        this.addressCodeList.countyCode = shiObj.children[data[2]].value;
      }
      if (data[3] !== undefined) {
        this.addressCodeList.townNo =
          shiObj.children[data[2]].children[data[3]].value;
      }
      try {
        let zcode = findPostCode(shengName, shiName, quName);
        // console.log(zcode);
        this.addressCodeList.zipCode = zcode;
      } catch (error) {}
      this.$emit("addresssConfirm", this.addressCodeList);
    },
    //取消,关闭弹窗
    cancle() {
      this.$emit("cancle");
    },
  },
};
</script>
<style lang="less" scoped>
.load-css {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 120px 0;
}
</style>
